package com.exsun.ex1077.ui.activity.login

import android.os.Bundle
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import com.exsun.ex1077.app.Constant
import com.exsun.ex1077.databinding.ActivityLoginBinding
import com.exsun.ex1077.extend.toPhotoUrl
import com.exsun.ex1077.network.ApiUrl
import com.exsun.ex1077.ui.activity.main.MainActivity
import com.exsun.ex1077.ui.activity.web.WebActivity
import com.exsun.ex1077.ui.popup.MsgPopup
import com.zhangping.fastjetpack.base.BaseActivity
import com.zhangping.fastjetpack.extend.*
import com.zhangping.fastjetpack.extend.base.parseState
import com.zhangping.fastjetpack.extend.view.applyUnderline
import com.zhangping.fastjetpack.network.upgrade.pgyer.PgyerCheckUpdate
import kotlinx.android.synthetic.main.activity_login.*

/**
 * 描述：登录页面
 * 作者：ZhangPing
 * 创建时间：2022/8/5 16:57
 */
class LoginActivity: BaseActivity<LoginViewModel, ActivityLoginBinding>() {

    override fun enableDoubleBackExit() = true

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.vm = mViewModel
        super.initView(savedInstanceState)
        if (getCacheString("token").isNotEmpty()) {
            toActivity<MainActivity>(finish = true)
            return
        }
        val rememberPassword = getCacheBoolean("remember");
        mViewModel.apply {
            account.set(getCacheString("account"))
            agreement.set(getCacheBoolean("agreement"))
            if (rememberPassword){
                remember.set(true);
                password.set(getCacheString("password"))
            }
        }
        eye.setOnClickListener {
            mViewModel.apply {
                pwdShow.reversal()
                if (pwdShow.get()) {
                    passwordEdit.transformationMethod = HideReturnsTransformationMethod.getInstance()
                } else {
                    passwordEdit.transformationMethod = PasswordTransformationMethod.getInstance()
                }
                passwordEdit.setSelection(password.get().length)
            }
        }
        remember.setOnClickListener { mViewModel.remember.reversal() }
        agreement.setOnClickListener { mViewModel.agreement.reversal() }
//        register.applyUnderline()
//        register.setOnClickListener {
//            toActivity<RegisterActivity>(Bundle().apply{
//                putInt("pageType", Constant.PAGE_TYPE_REGISTER_PERSON)
//            })
//        }
        userAgreement.setOnClickListener {
            toActivity<WebActivity>(Bundle().apply {
                putString("title", "用户协议")
                putString("url", ApiUrl.USER_AGREEMENT)
            })
        }
        privacyAgreement.setOnClickListener {
            toActivity<WebActivity>(Bundle().apply {
                putString("title", "隐私政策")
                putString("url", ApiUrl.PRIVACY_AGREEMENT)
            })
        }
        login.setOnClickListener {
            mViewModel.apply {
                if (account.get().isEmpty()){
                    "账号为空！".showToast()
                    return@setOnClickListener
                }
                if (password.get().isEmpty()){
                    "密码为空！".showToast()
                    return@setOnClickListener
                }
                if (!agreement.get()){
                    "请阅读并同意勾选用户协议和隐私条款！".showToast()
                    return@setOnClickListener
                }
                login()
            }
        }
        //检查更新
        PgyerCheckUpdate.checkUpdate(this)
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.apply {
            loginResult.observe(this@LoginActivity, {
                parseState(it, {
                    if (remember.get()){
                        "password".putCache(password.get())
                    }
                    "token".putCache(it.token)
                    "remember".putCache(remember.get())
                    "account".putCache(account.get())
                    "name".putCache(it.name)
                    "companyName".putCache(it.companyName)
                    "photo".putCache(it.photo.toPhotoUrl())
                    "agreement".putCache(true)
                    toActivity<MainActivity>(finish = true)
                }, {
                    MsgPopup.error(activity, it.errorMsg)
                })
            })
        }
    }

}